home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’96 / Sessions ’96 / AI and The Mac / Demos / TravSales.Think / PathGraph.cp < prev    next >
Encoding:
Text File  |  1996-06-22  |  4.1 KB  |  158 lines  |  [TEXT/MPS ]

  1. //    PathGraph.cp
  2. // Copyright © 1992 Emergent Behavior. All rights reserved.
  3.  
  4. #ifndef _PATHGRAPH_
  5.     #include "PathGraph.h"
  6. #endif
  7.  
  8. #ifndef _GAFAILURE_
  9.     #include "GAFailure.h"
  10. #endif
  11.  
  12. //==========================================================
  13. TPathGraph::TPathGraph( void )
  14.     :    fGrafPort( NIL ),
  15.         fCityMap( NIL ),
  16.         fPlotter( NIL )
  17. {
  18.  
  19. }
  20. //----------------------------------------------------------
  21. TPathGraph::TPathGraph( const TPathGraph& sourcePathGraph )
  22.     :    fGrafPort( sourcePathGraph.fGrafPort ),
  23.         fQDRect( sourcePathGraph.fQDRect ),
  24.         fCityMap( sourcePathGraph.fCityMap ),
  25.         fPlotter( sourcePathGraph.fPlotter )
  26. {
  27. }
  28. //----------------------------------------------------------
  29. TPathGraph::TPathGraph( const TCityMap& aCityMap, GrafPtr grafPort, Rect qdRect )
  30.     :    fGrafPort( grafPort ),
  31.         fQDRect( qdRect ),
  32.         fCityMap( (TCityMap*)&aCityMap )
  33. {
  34.  
  35.     Point* cityLocations    = aCityMap.GetCityLocations();
  36.     short largestDimension    = this->FindLargestDimension( cityLocations );
  37.      largestDimension +=10;
  38.     HugeRect scaledRect( -largestDimension, largestDimension, largestDimension, -largestDimension );
  39.     TPlotter* plotter     = new TPlotter( &qdRect, &scaledRect, grafPort);
  40.     FailNIL(plotter);
  41.     fPlotter            = plotter;
  42. }
  43. //----------------------------------------------------------
  44. TPathGraph::~TPathGraph( void )
  45. {
  46.     delete fPlotter;
  47. }
  48.  
  49. //----------------------------------------------------------
  50. TPathGraph*
  51. TPathGraph::Clone( void )
  52. {
  53.     TPathGraph* copy = new TPathGraph(*this);
  54.     FailNIL(copy);
  55.     return copy;
  56. }
  57.  
  58. //----------------------------------------------------------
  59. void
  60. TPathGraph::SetGrafPort( GrafPtr grafPort )
  61. {
  62.     fGrafPort = grafPort;
  63. }
  64.  
  65. //----------------------------------------------------------
  66. void
  67. TPathGraph::SetQDRect( Rect qdRect )
  68. {
  69.     fQDRect = qdRect;
  70. }
  71.  
  72. //----------------------------------------------------------
  73. void
  74. TPathGraph::PlotCityLocations( void )
  75. {
  76.     short kPenHeight            = 3;
  77.     short kPenWidth                = 3;
  78.     TPen* aPen                     = new TPen( *fPlotter, blackColor );
  79.     FailNIL(aPen);
  80.     short numCities             = fCityMap->GetNumberCities();
  81.     Point* cityLocationArray    = fCityMap->GetCityLocations();
  82.     
  83.     aPen->SetSize( kPenWidth, kPenHeight );
  84.  
  85.     for ( short cityIndex = 0; cityIndex < numCities; ++cityIndex )
  86.         aPen->DotAt( cityLocationArray[ cityIndex ].h, cityLocationArray[ cityIndex ].v );
  87.     delete aPen;
  88. }
  89. //----------------------------------------------------------
  90. void
  91. TPathGraph::PlotRoute( Importance* importanceArray )
  92. {
  93.     TPen*     aPen                 = new TPen( *fPlotter, redColor );
  94.     FailNIL(aPen);
  95.     short    numCities             = fCityMap->GetNumberCities();
  96.     Point*     cityLocationArray    = fCityMap->GetCityLocations();
  97.     Route    theRoute            = fCityMap->SortCities( importanceArray );
  98.     City    currentCity;
  99.     
  100.     currentCity    =theRoute[ 0 ];
  101.     aPen->Up();
  102.     aPen->Goto( cityLocationArray[ currentCity ].h, cityLocationArray[ currentCity ].v );
  103.  
  104.     aPen->Down();
  105.     for ( short cityIndex = 1; cityIndex < numCities; ++cityIndex )
  106.     {
  107.         currentCity    =theRoute[ cityIndex ];
  108.         aPen->Goto( cityLocationArray[ currentCity ].h, cityLocationArray[ currentCity ].v ); 
  109.     }
  110.     
  111.     currentCity    =theRoute[ 0 ];
  112.     aPen->Goto( cityLocationArray[ currentCity ].h, cityLocationArray[ currentCity ].v );
  113.  
  114.     
  115.     delete aPen;
  116. }
  117.  
  118. //----------------------------------------------------------
  119. GrafPtr
  120. TPathGraph::GetGrafPort( void )
  121. {
  122.     return fGrafPort;
  123. }
  124. //----------------------------------------------------------
  125. Rect
  126. TPathGraph::GetQDRect( void )
  127. {
  128.     return fQDRect;
  129. }
  130. //----------------------------------------------------------
  131. TPlotter*
  132. TPathGraph::GetPlotter( void )
  133. {
  134.     return fPlotter;
  135. }
  136. //----------------------------------------------------------
  137. short
  138. TPathGraph::FindLargestDimension( Point* cityLocationArray )
  139. {
  140.     short    numCities             = fCityMap->GetNumberCities();
  141.     short     largestDimension     = 0;
  142.  
  143.     for (short cityIndex = 0; cityIndex < numCities; ++cityIndex )
  144.     {
  145.         if (cityLocationArray[ cityIndex ].h > largestDimension)
  146.             largestDimension    = cityLocationArray[ cityIndex ].h;
  147.         if (cityLocationArray[ cityIndex ].v > largestDimension)
  148.             largestDimension    = cityLocationArray[ cityIndex ].v;
  149.     }
  150.     return largestDimension;
  151. }
  152. //----------------------------------------------------------
  153. TCityMap*
  154. TPathGraph::GetCityMap( void )
  155. {
  156.     return fCityMap;
  157. }
  158.